#include<iostream>
#include<thread>
#include<mutex>
#include<condition_variable>
using namespace std;

mutex mtx;
condition_variable cv;
int tCount = 0;

void threadA() {
    for (int i = 0; i < 5; ++i) {
        unique_lock<mutex> lock(mtx);
        cv.wait(lock, []{ return tCount % 2 == 0; });
        cout << "A";
        tCount++;
        cv.notify_one();
    }
}

void threadB() {
    for (int i = 0; i < 5; ++i) {
        unique_lock<mutex> lock(mtx);
        cv.wait(lock, []{ return tCount % 2 == 1; });
        cout << "B ";
        tCount++;
        cv.notify_one();
    }
}

int main() {
    thread A(threadA);
    thread B(threadB);

    A.join();
    B.join();
    return 0;
}
